package com.yiren.backstage.communication.utils;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.text.TextUtils;
import android.util.Log;

import java.lang.reflect.Method;
import java.util.Set;
import java.util.UUID;

public class BluetoothUtil {

    public static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    private static BluetoothUtil instance;
    private BluetoothAdapter adapter;

    public static BluetoothUtil getInstance() {
        if (instance == null) {
            instance = new BluetoothUtil();
        }
        return instance;
    }

    private BluetoothUtil() {
        adapter = BluetoothAdapter.getDefaultAdapter();
    }

    public boolean isBluetoothAvailable() {
        return adapter != null;
    }

    public boolean isBluetoothEnable() {
        return adapter != null && adapter.isEnabled();
    }

    public void enableBluetooth(boolean enable) {
        if (adapter != null) {
            if (enable) {
                adapter.enable();
            } else {
                adapter.disable();
            }
        }
    }

    public Set<BluetoothDevice> getBondedDevices() {
        if (adapter != null) {
            return adapter.getBondedDevices();
        }
        return null;
    }

    public BluetoothDevice theBluetoothDevice(String name, String address) {
        if (adapter != null) {
            Set<BluetoothDevice> pairedDevices = adapter.getBondedDevices();
            for (BluetoothDevice device : pairedDevices) {
                if (device.getName().equals(name) && device.getAddress().equals(address)) {
                    return device;
                }
            }
        }
        return null;
    }

    /**
     * 判断给定的设备mac地址是否已连接经典蓝牙
     *
     * @param macAddress 设备mac地址,例如"78:02:B7:01:01:16"
     * @return
     */
    public static boolean isConnectClassicBT(String macAddress) {
        if (TextUtils.isEmpty(macAddress)) {
            return false;
        }
        final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        Class<BluetoothAdapter> bluetoothAdapterClass = BluetoothAdapter.class;//得到BluetoothAdapter的Class对象
        try {
            //是否存在连接的蓝牙设备
            Method method = bluetoothAdapterClass.getDeclaredMethod("getConnectionState", (Class[]) null);
            //打开权限
            method.setAccessible(true);
            int state = (int) method.invoke(bluetoothAdapter, (Object[]) null);
            if (state == BluetoothAdapter.STATE_CONNECTED) {
                Log.d("test", "BluetoothAdapter.STATE_CONNECTED");
                Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
                Log.d("test", "devices:" + devices.size());
                for (BluetoothDevice device : devices) {
                    Method isConnectedMethod = BluetoothDevice.class.getDeclaredMethod("isConnected", (Class[]) null);
                    method.setAccessible(true);
                    boolean isConnected = (boolean) isConnectedMethod.invoke(device, (Object[]) null);
                    if (isConnected) {
                        return macAddress.contains(device.getAddress());
                    } else {
                        Log.d("test", device.getName() + " connect false(" + device.getAddress() + ")");
                    }
                }
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
